package com.wyp168.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName IsValid
 * @Description TODO
 * @Author wyp168
 * @Date 2022/6/24 14:50
 */
public class IsValid {
    public static void main(String[] args) {
        /**
         * 测试List集合的add方法和remove方式是否会对size有影响
         */

//        List<String> list = new ArrayList<>();
//        System.out.println("初始化list集合的长度  "  + list.size());
//        list.add("23");
//        System.out.println("添加一个元素后的集合的长度   " + list.size());
//        list.remove(0);
//        System.out.println("删除一个元素后的集合长度   " + list.size());

        String muohao = "(])";
        System.out.println(isValid(muohao));
    }

    public static boolean isValid(String s) {
        char[] sc = s.toCharArray();
        List<Character> stack = new ArrayList<>();
        for (char item : sc) {
            if (item == '{' || item == '[' || item == '(') {
                stack.add(item);
            }
            if (item == '}' || item == ']' || item == ')') {
                if (stack.size() == 0) {
                    return false;
                }
                switch (stack.get(stack.size() - 1)) {
                    case 40:
                        if (item == ')')
                            stack.remove(stack.size() - 1);
                        else {
                            return false;
                        }
                        break;
                    case 91:
                        if (item == ']')
                            stack.remove(stack.size() - 1);
                        else {
                            return false;
                        }
                        break;
                    case 123:
                        if (item == '}')
                            stack.remove(stack.size() - 1);
                        else {
                            return false;
                        }
                        break;
                }
            }
        }
        if (stack.size() == 0 ) {
            return true;
        }else {
            return false;
        }
    }
}
